var express = require("express")
var router = express.Router()
var QcloudSms = require("qcloudsms_js")
var connection = require("../../../db/sql")
var jwt = require("jsonwebtoken")
const bcrypt = require("bcryptjs")
var homeSql = require("./homeSql")

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" })
})

/*获取首页数据值*/
router.get("/api/home/gethomedata", function (req, res, next) {
  connection.query(homeSql.selectHomeData(), function (err, resluts) {
    if (err) {
      res.send({
        msg: "查询错误",
        data: err,
      })
    }

    res.send({
      status: 200,
      msg: "查询成功",
      data: resluts,
    })
  })
})

router.post("/api/home/shopdetail", function (req, res, next) {
  let id = req.body.id
  let token = req.headers.token
  let tokenObj = jwt.decode(token)
  connection.query(homeSql.selectShopDetail(id), function (err, resluts) {
    if (err) {
      res.send({
        msg: "查询错误",
        data: err,
      })
    }
    connection.query(
      homeSql.selectShopMessage(tokenObj),
      function (err, reslut) {
        if (err) {
          res.send({
            msg: "查询错误",
            data: err,
          })
        }
        res.send({
          status: 200,
          msg: "查询成功",
          data: resluts,
          num: reslut.length,
        })
      }
    )
  })
})

// 获取轮播数据
router.get("/api/home/getrationdata", function (req, res, next) {
  connection.query(homeSql.selectRationData(), function (err, resluts) {
    if (err) {
      res.send({
        msg: "查询错误",
        data: err,
      })
    }

    res.send({
      status: 200,
      msg: "查询成功",
      data: resluts,
    })
  })
})

module.exports = router
